package com.qiaomeng.flutter.fluttet_clipboard;

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;

public class Handler {
    private Context context;

    public Handler(Context context) {
        this.context = context;
    }

    public String getText() {
        ClipboardManager c = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData data = c.getPrimaryClip();
        if (data == null) {
            return "";
        }

        if ((c.getPrimaryClipDescription() != null && c.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) || data.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            String str = data.getItemAt(0).getText().toString();
            return str;
        }

        return "";

    }


    public void setText(String text) {
        if (text == null) {
            text = "";
        }
        ClipboardManager c = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText(null, text);
        c.setPrimaryClip(clipData);
    }


}
